package
{

    import com.bit101.components.PushButton;

    import copyengine.ane.AmrAudioRecordExtension;

    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filesystem.File;

    public class DemoAmrAudioRecord extends Sprite
    {

        private var mRecordBtn:PushButton;
        private var mStopRecordBtn:PushButton;
        private var mPlayBtn:PushButton;
        private var mStopPlayBtn:PushButton;
        private var mAMRFileULR:String;

        public function DemoAmrAudioRecord()
        {
            super();
            initialize();
        }

        //=================================//
        //=== Initialize & Dispose ========//
        //=================================//

        private function initialize():void
        {
            mRecordBtn = new PushButton(this, 300, 60, "Record", onClickRecordBtn);
            mStopRecordBtn = new PushButton(this, 300, 150, "StopRecord", onClickStopBtn);
            mPlayBtn = new PushButton(this, 300, 220, "Play", onClickPlayBtn);
            mStopPlayBtn = new PushButton(this, 300, 320, "StopPlay", onClickStopPlayBtn);


            mRecordBtn.scaleX = mRecordBtn.scaleY = 1.2;
            mStopRecordBtn.scaleX = mStopRecordBtn.scaleY = 1.2;
            mPlayBtn.scaleX = mPlayBtn.scaleY = 1.2;
            mStopPlayBtn.scaleX = mStopPlayBtn.scaleY = 1.2;
        }

        private function onClickStopPlayBtn(e:Event):void
        {
            trace("On Click Stop Play Btn");
            AmrAudioRecordExtension.instance.stopAmr();
        }

        private function onClickPlayBtn(event:MouseEvent):void
        {
            trace("On Click Play Btn");
            if (File.applicationDirectory.resolvePath("tmp/Record1.amr").exists)
            {
                trace("File is Exists start play");
                AmrAudioRecordExtension.instance.playAmr(File.applicationDirectory.resolvePath("tmp/Record1.amr").nativePath,
                                                         1,
                                                         onPlayAMRComplate);
            }
            else
            {
                trace("Can't play file not exists");
            }
        }

        private function onPlayAMRComplate():void
        {
            trace("AMR Played Finish!");
        }


        private function onClickRecordBtn(event:MouseEvent):void
        {
            AmrAudioRecordExtension.instance.startRecord("Record1");
            trace("On Click Record Btn");
        }

        private function onClickStopBtn(event:MouseEvent):void
        {
            AmrAudioRecordExtension.instance.stopRecord(onRecordFinish);
        }

        private function onRecordFinish(_fileURL:String):void
        {
            trace("On Record Finish, File: " + _fileURL);
            AmrAudioRecordExtension.instance.toAmr(onToAMRFinish);
        }

        private function onToAMRFinish(_fileURL:String):void
        {
            var orginalFile:File = new File(_fileURL);
            var tempFile:File = File.createTempFile();
            trace("TempFile is: " + tempFile.nativePath);
            if (orginalFile.exists)
            {
                orginalFile.copyTo(tempFile, true);
                mAMRFileULR = tempFile.nativePath;
                trace("On To AMR Finish! File: " + mAMRFileULR);
            }
            else
            {
                trace("AMR File Not Exists! File: " + _fileURL);
            }


        }

    }
}
